package com.chat.module.email.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.chat.module.user.mapper.UserMapper;
import com.chat.module.user.service.impl.UserServiceImpl;
import com.comment.dubbo.service.SystemEmailsService;
import com.chat.module.email.entity.PlayerEmailAssociation;
import com.chat.module.email.entity.SystemEmails;
import com.chat.module.email.mapper.PlayerEmailAssociationMapper;
import com.chat.module.email.vo.SystemEmailVo;
import com.chat.module.email.mapper.SystemEmailsMapper;
import com.comment.dubbo.service.CorePropServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * (SystemEmails)表服务实现类
 *
 * @author makejava
 * @since 2024-11-05 11:20:16
 */
@Service("systemEmailsService")
@Slf4j
@DubboService(interfaceClass = SystemEmailsService.class)
public class SystemEmailsServiceImpl extends ServiceImpl<SystemEmailsMapper, SystemEmails>
        implements SystemEmailsService {

    @Resource
    private SystemEmailsMapper systemEmailsDao;

    @Resource
    private PlayerEmailAssociationMapper playerEmailAssociationDao;


    @DubboReference
    private CorePropServiceClient corePropServiceClient;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserServiceImpl userServiceImpl;
    @Transactional
    @Override
    public boolean sendEmail(
            String title,
            String content,
            Integer expirationDay,
            List<Long> uidList,
            Map<String, Object> attachment
    ) {
        // 1.发送邮件
        SystemEmails systemEmails = new SystemEmails();
        systemEmails.setTitle(title);
        systemEmails.setContent(content);
        systemEmails.setExpirationDay(expirationDay);
        systemEmails.setAttachment(attachment);

        systemEmails.setCreatedAt(LocalDateTime.now());
        if (systemEmails.getExpirationDay() == null) {
            // 不设置过期天数，默认就30天过期
            systemEmails.setExpirationDay(30);
        }
        systemEmailsDao.insert(systemEmails);


        uidList.forEach(uid -> {
            userServiceImpl.inspect(uid);
            // 一个邮件发给指定多个用户
            PlayerEmailAssociation playerEmailAssociation = new PlayerEmailAssociation();

            playerEmailAssociation.setPlayerUid(uid);
            playerEmailAssociation.setSystemEmailId(systemEmails.getId());
            // 设置邮件读取状态是未读
            playerEmailAssociation.setReadStatus(0);
            // 设置附件领取状态是未领取
            playerEmailAssociation.setAttachmentClaimed(0);
            playerEmailAssociationDao.insert(playerEmailAssociation);
        });
        return true;
    }

    /**
     * 获取当前用户所有系统邮件
     */
    public List<SystemEmailVo> getALlSystemEmailByUid(Long playerUid) {
        userServiceImpl.inspect(playerUid);
        return systemEmailsDao.listSystemEmailVo(playerUid);
    }

    /**
     * 用户点击了系统邮件，就表示该邮件是已读了
     * 全部已读和已读都走这个业务 看有没有邮件id，没有邮件id就是全部已读
     */
    public Boolean updateReadStatus(
            Long playerUid,
            Long systemEmailId
    ) {
        LambdaQueryWrapper<PlayerEmailAssociation> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(PlayerEmailAssociation::getSystemEmailId, systemEmailId);
        updateWrapper.eq(systemEmailId == null, PlayerEmailAssociation::getPlayerUid, playerUid);
        // 表示系统邮件已读
        PlayerEmailAssociation update = new PlayerEmailAssociation();
        update.setReadStatus(1);
        return playerEmailAssociationDao.update(update, updateWrapper) > 0;
    }

    /**
     * 删除当前用户的已读邮件  当前邮件
     */
    public Boolean deleteAlreadyReader(
            Long playerUid,
            Long systemEmailId
    ) {
        LambdaQueryWrapper<PlayerEmailAssociation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlayerEmailAssociation::getSystemEmailId, systemEmailId);
        wrapper.eq(systemEmailId == null, PlayerEmailAssociation::getPlayerUid, playerUid);
        // 表示系统邮件已读
        PlayerEmailAssociation update = new PlayerEmailAssociation();
        update.setReadStatus(2);
        return playerEmailAssociationDao.update(update, wrapper) > 0;
    }


    @SuppressWarnings("unchecked")
    public PlayerEmailAssociation updateAttachmentClaimed(
            Long playerId, Long playerEmailId
    ) {
        return null;
    }


}

